﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using H2.NET.Controls;
using H2.NET.Data.Common;
using H2.NET.Business;


namespace H2.NET.Web.Pages.MDC
{
    public partial class TWTabInsPlan : TaskControl
    {
        #region 属性 Public
        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI["Status~" + Key] == null)
                {
                    StoreAPI["Status~" + Key] = new MDCTabStatus();
                }

                return (MDCTabStatus)StoreAPI["Status~" + Key];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI["Status~" + Key] = value;
            }
        }

        private string CustomerCode
        {
            get
            {
                return ViewState["CustomerCode"] == null ? "" : (string)ViewState["CustomerCode"];

            }
            set { ViewState["CustomerCode"] = value; }
        }

        private string CustomerName
        {
            get
            {
                return ViewState["CustomerName"] == null ? "" : (string)ViewState["CustomerName"];

            }
            set { ViewState["CustomerName"] = value; }
        }

        public string ProductionLine
        {
            get
            {
                return ViewState["ProductionLine"] == null ? "" : (string)ViewState["ProductionLine"];

            }
            set { ViewState["ProductionLine"] = value; }
        }

        public string Color
        {
            get
            {
                return ViewState["Color"] == null ? "" : (string)ViewState["Color"];

            }
            set { ViewState["Color"] = value; }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }

        public bool IsLoaded
        {
            get
            {
                return ViewState["IsLoaded"] == null ? false : (bool)ViewState["IsLoaded"];
            }
            set { ViewState["IsLoaded"] = value; }
        }
        #endregion
        #region 事件 Page_Load(页面加载)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Status.IsUsed)
            {
                return;
            }

            bool flg = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;

            if ((Page as PMDC).Actions.SelectedItem.Text == "2, Revision" && flg)
            {
                SetForeColor(System.Drawing.Color.Blue, Group_Counter_L);
            }
            else
            {
                SetForeColor(System.Drawing.Color.Black, Group_Counter_L);
            }
            if (flg)
            {
                SetForeColor(System.Drawing.Color.Blue, SAP_Code_L, Plant_L, Work_center_L, Status_L);
            }
            else
            {
                SetForeColor(System.Drawing.Color.Black, Group_Counter_L,SAP_Code_L, Plant_L, Work_center_L);
            }
            gv_Inspection.DynamicColumn.Add(new BoundColumn2("00", "Item", BoundColumnType2.Label, 34, Align.Left, flg, "No", InputType.None, null));
            gv_Inspection.DynamicColumn.Add(new BoundColumn2("01", "QN/QL", BoundColumnType2.DropDownList, 70, Align.Left, flg, "QN_QL", InputType.None,  new string[3] { "", "QN-定量特性", "QL-定性特性" }));
            gv_Inspection.DynamicColumn.Add(new BoundColumn2("02", "MstrChar Plant", BoundColumnType2.DropDownList, 60, Align.Left, flg, "MstrChar_Plant", InputType.None, new string[3] { "", "KT1P", "KT2P" }));
            gv_Inspection.DynamicColumn.Add(new BoundColumn2("03", "MstrChar Code", BoundColumnType2.TextBox, 60, Align.Left, flg, "MstrChar_Code", InputType.None, null));
            gv_Inspection.DynamicColumn.Add(new BoundColumn2("04", "Lower SpecLimit", BoundColumnType2.TextBox, 60, Align.Left, flg, "Lower_SpecLimit", InputType.Numeric, null));
            gv_Inspection.DynamicColumn.Add(new BoundColumn2("05", "Upper SpecLimit", BoundColumnType2.TextBox, 60, Align.Left, flg, "Upper_SpecLimit", InputType.Numeric, null));
            gv_Inspection.DynamicColumn.Add(new BoundColumn2("06", "Decimal number", BoundColumnType2.TextBox, 60, Align.Left, flg, "Decimal_Number", InputType.Numeric, null));
            gv_Inspection.DynamicColumn.Add(new BoundColumn2("07", "Long Text", BoundColumnType2.TextBox, 120, Align.Left, flg, "Long_Text", InputType.None, null));
            //gv_Inspection.Width = 1200;
            SetEnable(flg, Add, Delete, Group_Counter, Old_Material_Code, Plant, SAP_Code, InsPlan_Status, Work_center);
            if (!flg)
            {
                foreach (Control cr in this.Controls)
                {
                    if (cr is TextBox)
                    {
                        TextBox tb = (TextBox)cr;
                        tb.ForeColor = System.Drawing.Color.DimGray;
                        if (!tb.Enabled)
                        {
                            tb.Enabled = true;
                            tb.ReadOnly = true;
                        }

                    }

                }
            }
            if (!Page.IsPostBack)
            {
                InitControls();
            }
           SetToolTip();
        }
        #endregion
        #region 方法 Save(保存数据库)
        public void Save()
        {
            if (!Status.IsEdited)
            {
                return;
            }
            bool flag = false;
            foreach (TW_mdc_Insplan ccc in (List<TW_mdc_Insplan>)gv_Inspection.DataSource)
            {
                if (string.IsNullOrEmpty(ccc.Decimal_Number) && string.IsNullOrEmpty(ccc.Upper_SpecLimit) && string.IsNullOrEmpty(ccc.QN_QL) && string.IsNullOrEmpty(ccc.Lower_SpecLimit) && string.IsNullOrEmpty(ccc.MstrChar_Code) && string.IsNullOrEmpty(ccc.MstrChar_Plant) )
                {
                    flag = true;
                    continue;
                }
                ccc.Process = TaskAPI.Process;
                ccc.Incident = TaskAPI.Incident;
                ccc.UserName = UserAPI.UserName;
                ccc.Time = System.DateTime.Now;
                ccc.Status = 1;
                ccc.Group_Counter = Group_Counter.Text;
                ccc.Old_Material_Code = Old_Material_Code.Text;
                ccc.Plant = Plant.SelectedValue;
                ccc.SAP_Code = SAP_Code.Text;
                ccc.InsPlan_Status = InsPlan_Status.SelectedValue;
                ccc.Work_center = Work_center.SelectedValue;
                Storage.Save(ccc);
               
                //string s = ccc.ProductionLine + ccc.Color;
                //TaskAPI["ProductionLine"] = ccc.ProductionLine;
                //TaskAPI["Color"] = ccc.Color;
            }
            if (flag)
            {
                TW_mdc_Insplan ccc = new TW_mdc_Insplan();
                ccc.Process = TaskAPI.Process;
                ccc.Incident = TaskAPI.Incident;
                ccc.UserName = UserAPI.UserName;
                ccc.Time = System.DateTime.Now;
                ccc.Status = 1;
                ccc.Group_Counter = Group_Counter.Text;
                ccc.Old_Material_Code = Old_Material_Code.Text;
                ccc.Plant = Plant.SelectedValue;
                ccc.SAP_Code = SAP_Code.Text;
                ccc.InsPlan_Status = InsPlan_Status.SelectedValue;
                ccc.Work_center = Work_center.SelectedValue;
                Storage.Save(ccc);
            }
        }
        #endregion
        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {
            List<TW_mdc_Insplan> sefi_1_s = (List<TW_mdc_Insplan>)gv_Inspection.DataSource;
            TW_mdc_Insplan sefi_1 = new TW_mdc_Insplan();
            sefi_1.No = sefi_1_s.Count + 1;
            sefi_1_s.Insert(sefi_1_s.Count, sefi_1);
            gv_Inspection.DataSource = sefi_1_s;
            gv_Inspection.DataBind();
        }
        #endregion
        #region 事件 Delete(删除行)
        protected void Delete_Click(object sender, EventArgs e)
        {
            List<TW_mdc_Insplan> base_1_s = (List<TW_mdc_Insplan>)this.gv_Inspection.DataSource;
            IList<TW_mdc_Insplan> base1 = (TW_mdc_Insplan.process == TaskAPI.Process & TW_mdc_Insplan.incident == TaskAPI.Incident).List<TW_mdc_Insplan>();
            if (base_1_s.Count == 1)
            {

                Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一个 Sheet ！',false)");
                return;

            }
            base_1_s.RemoveAt(gv_Inspection.ChangedRowIndex == -1 || gv_Inspection.ChangedRowIndex >= base_1_s.Count ? base_1_s.Count - 1 : gv_Inspection.ChangedRowIndex);
            bool flag = false;
            foreach (TW_mdc_Insplan base_1 in base1)
            {

                foreach (TW_mdc_Insplan base_2 in base_1_s)
                {
                    if (base_2.ID == base_1.ID)
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    Storage.Delete(base_1);
                }
                flag = false;
            }
            gv_Inspection.DataSource = base_1_s;
            gv_Inspection.DataBind();

        }
        #endregion
        #region 方法 InitControls(初始化)
        public void InitControls()
        {
            gv_Inspection.Key = Key;
            if (TaskAPI.IsInitiate)
            {
                List<TW_mdc_Insplan> mdc_spm_Determination1 = new List<TW_mdc_Insplan>();
                for (int i = 0; i < 5; i++)
                {
                    mdc_spm_Determination1.Add(new TW_mdc_Insplan());
                    mdc_spm_Determination1[i].No = i + 1;
                    //mdc_spm_Determination1[i].MstrChar_Plant = "0";
                }
                gv_Inspection.DataSource = mdc_spm_Determination1;
            }
            else
            {
                IList<TW_mdc_Insplan> mdc_spm_Determination1 = (TW_mdc_Insplan.process == TaskAPI.Process & TW_mdc_Insplan.incident == TaskAPI.Incident & TW_mdc_Insplan.status == 1).List<TW_mdc_Insplan>();
                if (mdc_spm_Determination1.Count == 0)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        mdc_spm_Determination1.Add(new TW_mdc_Insplan());
                        mdc_spm_Determination1[i].No = i + 1;
                        mdc_spm_Determination1[i].MstrChar_Plant = "0";
                    }
                }

                foreach (TW_mdc_Insplan tw_insplan in mdc_spm_Determination1)
                {
                    Group_Counter.Text = tw_insplan.Group_Counter;
                    Old_Material_Code.Text = tw_insplan.Old_Material_Code;
                    Plant.SelectedValue = tw_insplan.Plant;
                    SAP_Code.Text = tw_insplan.SAP_Code;
                    InsPlan_Status.SelectedValue = tw_insplan.InsPlan_Status;
                    Work_center.SelectedValue = tw_insplan.Work_center;
                }
                
                gv_Inspection.DataSource = mdc_spm_Determination1;
            }
        }
        #endregion
        #region 方法 SetToolTip(设置控件 ToolTip)
        public void SetToolTip()
        {

            if (TaskAPI.Process.Contains("TW"))
            {
                Old_Material_Code.ToolTip = "舊的物料號";
                SAP_Code.ToolTip = "需要建立檢驗計畫的 SAP Code";
                Plant.ToolTip = "需要建立檢驗計畫的工廠";
                Work_center.ToolTip = "工作中心";
                InsPlan_Status.ToolTip = "檢驗計畫的狀態";
                Usage_1.ToolTip = "任務清單使用";
                Inspection_Point_1.ToolTip = "檢驗點";
                Dynamic_mod_Level_1.ToolTip = "定義動態修改參數的級別";
                Profile_1.ToolTip = "參數的文件";
            }
 

        }

        #endregion
        #region 方法 gv_Inspection 数据取得
        protected void gv_Inspection_DataBinding(object sender, EventArgs e)
        {
            List<TW_mdc_Insplan> ins_plan_s = gv_Inspection.DataSource as List<TW_mdc_Insplan>;
            if (gv_Inspection.ChangedRowIndex >= 0 && gv_Inspection.ChangedRowIndex < ins_plan_s.Count
                                    && !string.IsNullOrEmpty(ins_plan_s[gv_Inspection.ChangedRowIndex].QN_QL)
                                    && ins_plan_s[gv_Inspection.ChangedRowIndex].QN_QL.Contains("定性"))
            {
                ins_plan_s[gv_Inspection.ChangedRowIndex].Upper_SpecLimit = "";
                ins_plan_s[gv_Inspection.ChangedRowIndex].Lower_SpecLimit = "";
            }
        }
        #endregion

        #region 方法 DataValidation(数据验证)
        public List<string> DataValidation()
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!(Status.IsUsed && TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit))
            {
                return new List<string>();
            }
            if (!(Page as PMDC).IP_Selected.Checked && !(Page as PMDC).IP_Selected.Visible)
            {
                return new List<string>();
            }
            List<string> vald_s = new List<string>();
            List<Regular> regs = new List<Regular>();
            int i = 1;

            t_Attachment list = (t_Attachment.process == TaskAPI.Process & t_Attachment.incident == TaskAPI.Incident).ListFirst<t_Attachment>();
            if (list != null)
            {
                return vald_s;
            }
            if ((Page as PMDC).Actions.SelectedItem.Text == "2, Revision")
            {
                regs.Add(new Regular(RegularExp.NotEmpty, Group_Counter.Text, "&nbsp;&nbsp; Group Counter为必填"));
            }
            regs.Add(new Regular(RegularExp.NotEmpty, SAP_Code.Text, "SAP Code 为必填"));
            regs.Add(new Regular(RegularExp.NotEmpty, this.Plant.Text, "Plant 为必填"));
            regs.Add(new Regular(RegularExp.NotEmpty, this.Work_center.Text, "Work center 为必填"));
            regs.Add(new Regular(RegularExp.NotEmpty, this.InsPlan_Status.SelectedValue, "Status 为必填"));
        
            foreach (TW_mdc_Insplan ccc in (List<TW_mdc_Insplan>)this.gv_Inspection.DataSource)
            {

                    if (string.IsNullOrEmpty(ccc.QN_QL) && string.IsNullOrEmpty(ccc.MstrChar_Code) && string.IsNullOrEmpty(ccc.MstrChar_Plant) )
                    {
                        continue;
                    }
                    //regs.Add(new Regular(RegularExp.NotEmpty, ccc.QN_QL, "QN/QL 为必填"));
  
                    regs.Add(new Regular(RegularExp.NotEmpty, ccc.QN_QL, "QN/QL 为必填"));
                    regs.Add(new Regular(RegularExp.NotEmpty, ccc.MstrChar_Plant, "MstrChar Plant 为必填"));
                    regs.Add(new Regular(RegularExp.NotEmpty, ccc.MstrChar_Code, "MstrChar Code 为必填"));
                    if (ccc.QN_QL.Contains("定量"))
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, ccc.Decimal_Number, "Decimal Number 为必填"));
                        regs.Add(new Regular(RegularExp.NotEmpty, ccc.Lower_SpecLimit, "Lower SpecLimit 为必填"));
                        regs.Add(new Regular(RegularExp.NotEmpty, ccc.Upper_SpecLimit, "Upper SpecLimit 为必填"));

                        double d1 = 0.0;
                        double d2 = 0.0;
                        try
                        {
                            double.TryParse(ccc.Lower_SpecLimit, out d1);
                        }
                        catch (Exception)
                        {

                            d1 = 0.0;
                        }
                        try
                        {
                            double.TryParse(ccc.Upper_SpecLimit, out d2);
                        }
                        catch (Exception)
                        {

                            d2 = 0.0;
                        }

                        if (d1 > d2)
                        {
                            regs.Add(new Regular(RegularExp.NotEmpty, "", "&nbsp;&nbsp;S" + (i + 1).ToString() + "-2 => 行" + ccc.No + "中 Upper SpecLimit 应大于 Lower SpecLimit "));
                        }

                        //int dn = 0;
                        //try
                        //{
                        //    int.TryParse(ccc.Decimal_Number.Trim(), out dn);
                        //}
                        //catch (Exception)
                        //{

                        //    dn = 0;
                        //}

                        //if (dn >= 0)
                        //{
                        //    regs.Add(new Regular(dn == 0 ? RegularExp.Digit0 : (dn == 1 ? RegularExp.Digit1 : (dn == 2 ? RegularExp.Digit2 : (dn == 3 ? RegularExp.Digit3 : (dn == 4 ? RegularExp.Digit4 : RegularExp.Digit5)))), ccc.Lower_SpecLimit.Trim().Replace("-", ""), "&nbsp;&nbsp;S" + (i + 1).ToString() + "-2 => 行" + ccc.No + "中 Lower SpecLimit 小数点应该在 Decimal number规定范围内"));
                        //    regs.Add(new Regular(dn == 0 ? RegularExp.Digit0 : (dn == 1 ? RegularExp.Digit1 : (dn == 2 ? RegularExp.Digit2 : (dn == 3 ? RegularExp.Digit3 : (dn == 4 ? RegularExp.Digit4 : RegularExp.Digit5)))), ccc.Upper_SpecLimit.Trim().Replace("-", ""), "&nbsp;&nbsp;S" + (i + 1).ToString() + "-2 => 行" + ccc.No + "中 Upper SpecLimit 小数点应该在 Decimal number规定范围内"));
                        //}
                    }

                    i++;
                
            }

            //if (i == 1)
            //{
            //    t_Attachment list = (t_Attachment.process == TaskAPI.Process & t_Attachment.incident == TaskAPI.Incident).ListFirst<t_Attachment>();
            //    if (list == null)
            //    {
            //        regs.Add(new Regular(RegularExp.NotEmpty, list, "&nbsp;&nbsp;" + "表单不能为空或者必须上传附件!"));
            //    }

            //}
            vald_s.AddRange(Validation(regs));

            if (vald_s.Count > 0)
            {
                vald_s.Insert(0, "<font color='#0000FF'><b>" + Status.Name + "</b></font>");
                vald_s.Add("&nbsp;&nbsp;-----------------------------------------");
            }

            return vald_s;
        }
        #endregion
    }
}